﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Pampa.Grid.UI.CustomControls;

namespace Pampa.MVC.Controllers
{
    public class CustomGridController : AbstractController
    {
        public JsonResult Refresh(string gridId)
        {
            return BuildResponse(() => CustomGrid.Refresh(gridId));
        }

        public JsonResult RefreshSamePage(string gridId)
        {
            return BuildResponse(() => CustomGrid.RefreshSamePage(gridId));
        }

        public JsonResult MoveTo(string gridId, int position)
        {
            return BuildResponse(() => CustomGrid.MoveTo(gridId, position));
        }

        public JsonResult UpdateConfiguration(string gridId, string configuration)
        {
            return BuildResponse(() => CustomGrid.UpdateConfiguration(gridId, configuration));
        }

        public JsonResult UpdateQuickFilter(string gridId, string configuration)
        {
            var configXml = HttpUtility.UrlDecode(configuration);
            return BuildResponse(() => CustomGrid.UpdateQuickFilter(gridId, configXml));
        }

        public JsonResult GetCurrentConfiguration(string gridId)
        {
            return BuildResponse(() => CustomGrid.GetCurrentConfiguration(gridId));
        }

        public JsonResult CleanFilters(string gridId)
        {
            return BuildResponse(() => CustomGrid.CleanFilters(gridId));
        }

        public JsonResult OrderBy(string gridId, string field)
        {
            return BuildResponse(() => CustomGrid.OrderBy(gridId, field));
        }

        public JsonResult Print(string gridId)
        {
            return BuildResponse(() => CustomGrid.Print(gridId));
        }

        public ActionResult ToExcel(string gridId, string filename)
        {
            CustomGrid grid = CustomGrid.LoadFromSession(gridId);
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".xls");
            return Content(grid.ToExcel(), "application/vnd.ms-excel", System.Text.Encoding.Default);
        }
    }
}
